/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.slidebox;

import java.util.List;

import javax.swing.Action;

import com.pearreview.util.review.Attendee;
import com.pearreview.util.review.AvailableAttendee;
import com.pearreview.util.review.IdArrayList;
import com.pearreview.util.review.IdList;

public class AvailableAttendeeSlideBoxPanel extends SlideBoxPanel<Attendee> {
	
	private static final long serialVersionUID = -5698706879289007778L;
	
	public AvailableAttendeeSlideBoxPanel(Action editAction) {
		super(editAction);
	}
	
	@Override
	protected AvailableAttendeeSlideBox createSlideBox(Attendee element,Action 
			deleteAction, Action editAction) {
		return new AvailableAttendeeSlideBox(element, deleteAction, editAction);
	}
	
	public void addToList(Attendee element, boolean update, boolean available, 
			Long preparationTime) {
		
		elementList.add(element);
		AvailableAttendeeSlideBox box = 
				createSlideBox(element, deleteAction, editAction);
		box.setPreperationTime(preparationTime);
		box.setAvailable(available);
		
		addToList(element, box, update);
	}
	
	public void endEdit(Attendee element, boolean available, 
			Long preparationTime) {	
		
		super.endEdit(element);
		int index = elementList.indexOf(element);
		if (index != -1) {
			((AvailableAttendeeSlideBox)boxList.get(index))
					.setAvailable(available);
			((AvailableAttendeeSlideBox)boxList.get(index))
					.setPreperationTime(preparationTime);
			((AvailableAttendeeSlideBox)boxList.get(index))
					.updateBox();
		}
	}
	
	public IdList<AvailableAttendee> getAvailableAttendees() {
		IdList<AvailableAttendee> list = new IdArrayList<AvailableAttendee>();
		
		for(SlideBox<?> box: boxList) {
			AvailableAttendeeSlideBox aBox = (AvailableAttendeeSlideBox)box;
			
			if (aBox.isAvailable()) {
				AvailableAttendee attendee = 
						new AvailableAttendee(aBox.getElement());
				attendee.setPreparationTime(aBox.getPreparationTime());
				list.add(attendee);
			}
		}
		
		return list;
	}
	
	public void setAvailableAttendees(List<AvailableAttendee> list) {
		for(AvailableAttendee aAttendee: list) {
			int index = elementList.indexOf(aAttendee.getAttendee());
			if (index != -1) {
				AvailableAttendeeSlideBox aBox = 
					(AvailableAttendeeSlideBox)boxList.get(index);
				aBox.setAvailable(true);
				
				aBox.setPreperationTime(aAttendee.getPreparationTime());
			}
		}
	}
	
	public AvailableAttendeeSlideBox getBox(Attendee attendee) {
		int index = elementList.indexOf(attendee);
		if (index != -1) {
			return (AvailableAttendeeSlideBox) boxList.get(index);
		}
		
		return null;
	}
	
	@Override
	protected void setEnabledBox(SlideBox<Attendee> box, boolean enabled) {
		box.setCheckEnabled(enabled);
		super.setEnabledBox(box, enabled);
	}
	
}
